<?php

$di = \application\units\Di::getInstance();

$params = [
    'id'       => $di->request->params('id'),
    'pid'      => $di->request->params('pid'),
    'type'     => $di->request->params('type'),
    'path'     => $di->request->params('path'),
    'resource' => $di->request->params('resource'),
    'title'    => $di->request->params('title'),
    'icon'     => $di->request->params('icon', ''),
    'remark'   => $di->request->params('remark', ''),
    'badge'    => $di->request->params('badge', ''),
    'weigh'    => $di->request->params('weigh'),
    'status'   => $di->request->params('status'),
];

if (!ctype_digit($params['pid'])) throw new LogicException('Invalid param pid');
if (!in_array($params['type'], [1, 2])) throw new LogicException('Invalid param type');
if ('' == $params['path']) throw new LogicException('Invalid param path');
if ('' == $params['resource']) throw new LogicException('Invalid param resource');
if ('' == $params['title']) throw new LogicException('Invalid param title');
if (!ctype_digit($params['weigh'])) throw new LogicException('Invalid param weigh');
if (!in_array($params['status'], [0, 1])) throw new LogicException('Invalid param status');

if ($params['pid'] > 0) {
    $hasPid = $di->db->find('select * from admin_resource where type=1 and id=:id', [
        'id' => $params['pid'],
    ]);
    if (!$hasPid) throw new LogicException('pid菜单不存在');
    
    $rows        = $di->db->query('select * from admin_resource where type=1');
    $ChildrenIds = $di->tree->init($rows)->getChildrenIds($params['id'], true);
    if (in_array($params['pid'], $ChildrenIds)) throw new LogicException('pid菜单不能为自己id或者是下级id在');
}

$hasUri = $di->db->find('select * from admin_resource where path=:path and id<>:id', [
    'path' => $params['path'],
    'id'   => $params['id'],
]);
if ($hasUri) throw new LogicException('路由已存在');

$hasResource = $di->db->find('select * from admin_resource where resource=:resource and id<>:id', [
    'resource' => $params['resource'],
    'id'       => $params['id'],
]);
if ($hasResource) throw new LogicException('资源已存在');

$row = $di->db->find('select * from admin_resource where id=:id ', [
    'id' => $params['id'],
]);
if (!$row) throw new LogicException('未找到记录');

$stat = $di->db->execute('update admin_resource set pid=:pid, type=:type, path=:path, resource=:resource, title=:title, icon=:icon, remark=:remark, badge=:badge, weigh=:weigh, status=:status, update_time=:update_time
                      where id=:id', [
    'id'          => $params['id'],
    'pid'         => $params['pid'],
    'type'        => $params['type'],
    'path'        => $params['path'],
    'resource'    => $params['resource'],
    'title'       => $params['title'],
    'icon'        => $params['icon'],
    'remark'      => $params['remark'],
    'badge'       => $params['badge'],
    'weigh'       => $params['weigh'],
    'status'      => $params['status'],
    'update_time' => date('Y-m-d H:i:s'),
]);

if ($stat->rowCount() <= 0) throw new PDOException('操作失败');

return [];